<?php

namespace Admin\Controller;

class AreaController extends AdminbaseController {

    protected $areaModel;

    function _initialize() {
        parent::_initialize();
        $this->areaModel = D("area");
    }

    function index() {

        $count = $this->areaModel->where()->count();
        $page = $this->page($count, 20);
        $list = $this->areaModel->alias("a")
            ->join("left join __REGION__ g on g.region_id = a.region_id")
            ->field("a.*, g.region_name")
            ->limit($page->firstRow . ',' . $page->listRows)->select();
       
        $this->assign("list", $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    function viewMap() {
        
        $this->display();
        
    }
    
    function add() {
        $this->display();
    }

    /**
     *  添加规则
     */
    function add_post() {
        if (IS_POST) {
           
            $data['name'] = I('post.name');
            $data['create_time'] = getCurrentDate();
            $data['region_id'] = I("post.regionId");

            $this->areaModel->add($data);

            $this->success("添加成功！", U('area/index'));
        }
    }

    /**
     *  编辑规则基本信息
     */
    function edit() {
        $id = I('get.id');
        $area = $this->areaModel->where(array('area_id' => $id))->find();
        $this->assign('area', $area);
        $this->display();
    }

    /**
     *  编辑规则基本信息
     */
    function edit_post() {
        if (IS_POST) {
            $id = I('post.id');

            $data = $this->areaModel->where(array('area_id' => $id))->find();

            $data['region_id'] = I("post.regionId");
            $data['name'] = I('post.name');

            $this->areaModel->where(array('area_id' => $id))->save($data);

            $this->success("更新成功！", U('area/index'));
        }
    }

    /**
     *  删除
     */
    function delete() {
        $id = intval(I('get.id'));
        $this->areaModel->delete($id);
        $this->success("删除成功！", U('area/index'));
    }

}
